package com.xdl.flytest.controller;

import com.xdl.flytest.bean.TestSuit;
import com.xdl.flytest.service.TestSuitService;
import com.xdl.flytest.util.JsonResponseMessage;
import com.xdl.flytest.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 80736
 * @Date: 2019/1/6 13:12
 * @Description:
 */
@Controller
@RequestMapping("/test")
public class TestSuitController {
    @Autowired
    private TestSuitService testSuitService;

    @GetMapping("/testsuits")
    public ModelAndView getTestSuits() {
        List<TestSuit> testSuits = testSuitService.getAllTestSuits();
        ModelAndView mav = new ModelAndView("test/page-testsuit");
        mav.addObject("testsuits", testSuits);
        return mav;
    }

    @GetMapping("/testsuit_list")
    @ResponseBody
    public Map<String, Object> getTestsuitList() {
        List<TestSuit> testSuits = testSuitService.getAllTestSuits();
        return new JsonResponseMessage(ResponseMsg.SUCCESS).addAttribute("testsuits", testSuits).returned();

    }

    @GetMapping("/testsuit")
    @ResponseBody
    public Map<String, Object> getTestSuit(@RequestParam("id") Integer testsuitId) {
        TestSuit testsuit = testSuitService.getTestSuitById(testsuitId);
        return new JsonResponseMessage(ResponseMsg.SUCCESS).addAttribute("testsuit", testsuit).returned();

    }

    @PostMapping("/testsuit")
    @ResponseBody
    public Map<String, Object> addTestSuit(@Validated TestSuit testSuit, BindingResult result) {
        testSuitService.addTestSuit(testSuit);
        // todo 校验
        return new JsonResponseMessage(ResponseMsg.SUCCESS).returned();
    }

    @PutMapping("/testsuit")
    @ResponseBody
    public Map<String, Object> updateTestSuit(@Validated TestSuit testSuit, BindingResult result) {
        testSuitService.updateTestSuit(testSuit);

        return new JsonResponseMessage(ResponseMsg.SUCCESS).returned();
    }

    @DeleteMapping("/testsuit")
    @ResponseBody
    public Map<String, Object> deleteTestSuit(@RequestParam("id") Integer testSuitId) {
        if (testSuitId != null) {
            testSuitService.deleteTestSuit(testSuitId);
            return new JsonResponseMessage(ResponseMsg.SUCCESS).returned();
        }

        return new JsonResponseMessage(ResponseMsg.PARAMETER_ERROR).returned();
    }


    @GetMapping("/testsuit/run")
    @ResponseBody
    public Map<String, Object> runTestSuit(@RequestParam("id") Integer id) {
        try {
            Integer reportId = testSuitService.runTestSuit(id);
            return new JsonResponseMessage(ResponseMsg.SUCCESS).addAttribute("report", reportId).returned();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new JsonResponseMessage(ResponseMsg.SERVER_ERROR).returned();
    }

}
